﻿using System;
using System.Windows;
using LEDContract;
using LEDHelper;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
public partial class LEDSVCWIN
{

    static WXLED wx = new WXLED(); 
 
    private void SaveToFile(byte[] value, string filePath)
    {
        System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate);
        fs.Write(value, 0, value.Length);
        fs.Flush();
        fs.Close();
    }
    private Bitmap BitmapFromByteArray(byte[] byteArray)
    {
        int n = 0;
        // Get the width
        uint x = (((uint)byteArray[n] * 256 + (uint)byteArray[n + 1]) * 256 + (uint)byteArray[n + 2]) * 256 + (uint)byteArray[n + 3];
        int width = (int)x;
        n += 4;
        // Get the height
        x = (((uint)byteArray[n] * 256 + (uint)byteArray[n + 1]) * 256 + (uint)byteArray[n + 2]) * 256 + (uint)byteArray[n + 3];
        int height = (int)x;
        n += 4;
        // Create the Bitmmap object
        Bitmap bmp = new Bitmap(width, height);
        // The pixels are stored in order by rows
        for (int j = 0; j < height; j++)
        {
            // Read the pixels for each row
            for (int i = 0; i < width; i++)
            {
                x = (((uint)byteArray[n] * 256 + (uint)byteArray[n + 1]) * 256 + (uint)byteArray[n + 2]) * 256 + (uint)byteArray[n + 3];
                bmp.SetPixel(i, j, Color.FromArgb((int)x));
                n += 4;
            }
        }
        return bmp;
    }
    public object SendLED(string ctype, Dictionary<string, string> para)
    {
        object result = null;
        int nLEDID = 1;
        string szBmpFile = "";
        LEDBlock destRect = new LEDBlock();
        LEDBlock sourceRect = new LEDBlock();
        int nEffectType = 0;
        int nSpeed = 0;
        int lTrasnparent = 0;
        string szText = "";
        int x1 = 0, y1 = 0, width = 1, height = 1;
        FontDescriptor lpFont = new FontDescriptor();
        ClockDescriptor lpClock = new ClockDescriptor();
        switch (ctype)
        {
            case "LED_SavePicture":
                szBmpFile = para["PICTURENAME"].ToString();
                string file = "D:/SXDFile/" + szBmpFile;
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                string patternStr = para["PVALUE"].ToString();
                byte[] ba = Convert.FromBase64String(patternStr);
                //Image  bmp = BitmapFromByteArray(ba);
                SaveToFile(ba,file);
                break;
            case "LED_RegLED":
                wx.LED_RegLED(para);
                break;
            case "LED_GetLEDList":
                result = wx.LED_GetLEDList();
                break;
            case "LED_CopyScreen":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_CopyScreen(nLEDID);
                break;
                
            /// <summary>
            /// 3.1	动态链接库初始化:
            /// 定义：int InsigmaLEDDriver_Init( void ); 
            /// 功能：初始化动态链接库，将需要进行DLL初始化的代码放在这个函数中.
            /// 输入：无.
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "InsigmaLEDDriver_Init":
                result = wx.InsigmaLEDDriver_Init();
                break;
            /// <summary>
            /// 3.2	清理动态链接库
            /// 定义：void InsigmaLEDDriver_Cleanup( void );
            /// 功能：卸载动态链接库，将需要进行DLL卸载的代码放在这个函数里，比如设备内存释放等
            /// 输入：无.
            /// 返回：无.
            /// </summary>
            /// <returns></returns>
            case "InsigmaLEDDriver_Cleanup":
                wx.InsigmaLEDDriver_Cleanup();
                result = 0;
                break;
            /// <summary>
            /// 3.3	检查LED设备是否正常
            /// 定义：int LED_IsDeviceOK( int nLEDID );
            /// 功能：检测指定的屏幕显示设备是否正常运行
            /// 输入：nLEDID - 设备ID；
            /// 返回：0表示正常， -1表示出现异常。
            /// </summary>
            /// <returns></returns>                   

            case "LED_IsDeviceOK":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_IsDeviceOK(nLEDID);
                break;
            /// <summary>
            /// 3.4	屏幕开关控制
            /// 定义: int LED_SwitchPower( int nLEDID, int nTurnOn );
            /// 功能：对远程显示屏设备进行开关控制
            /// 输入: nLEDID - 设备ID号
            /// nTurnOn – 1表示打开设备 0表示关闭设备
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_SwitchPower":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                int nTurnOn = System.Convert.ToInt32(para["POWER"].ToString());
                result = wx.LED_SwitchPower(nLEDID, nTurnOn);
                if (para.ContainsKey("OUTPUTIP"))
                {
                    result = wx.LEDGetIP(nLEDID);
                }
                break;
            /// <summary>
            /// 3.5	检查设备是否开启
            /// 定义: int LED_IsDeviceTurnOn( int nLEDID );
            /// 功能：查询远程显示设备是否打开
            /// 输入: nLEDID – 设备ID号
            /// 返回: 0 表示关闭， 1表示打开
            /// </summary>
            /// <returns></returns>
            case "LED_IsDeviceTurnOn":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_IsDeviceTurnOn(nLEDID);
                break;
            /// <summary>
            /// 3.6 检测设备是否离线
            /// 定义: int LED_IsDeviceOffline( int nLEDID );
            /// 功能: 检测指定的LED显示屏设备是否处于离线状态
            /// 输入: nLEDID – 设备ID号
            /// 返回: 0 表示离线 1 表示在线
            /// </summary>
            /// <returns></returns>
            case "LED_IsDeviceOffline":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_IsDeviceOffline(nLEDID);
                break;
            /// <summary>
            /// 3.7 设置屏幕的亮度值
            /// 定义: int LED_SetBright( int nLEDID, int lValue );
            /// 功能: 设置指定显示屏的屏幕亮度
            /// 输入: nLEDID – 设备ID号
            /// lValue – 亮度值
            /// 返回：成功返回0，失败返回-1.
            /// </summary>
            /// <returns></returns>
            case "LED_SetBright":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                int lValue = System.Convert.ToInt32(para["BRIGHT"].ToString());
                result = wx.LED_SetBright(nLEDID, lValue);
                break;
            /// <summary>
            /// 3.8 获取屏幕的亮度值
            /// 定义: int LED_GetBright( int nLEDID);
            /// 功能: 设置指定显示屏的屏幕亮度
            /// 输入: nLEDID – 设备ID号
            /// lValue – 亮度值
            /// 返回：成功返回0，失败返回-1.
            /// </summary>
            /// <returns></returns>
            case "LED_GetBright":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_GetBright(nLEDID);
                break;
            /// <summary>
            /// 3.9	显示屏重启
            /// 定义: int LED_Restart( int nLEDID );
            /// 功能: 对指定的显示屏进行重启操作(重启后如果显示屏内含有固化的帧就显示固化数据，否则不显示)
            /// 输入： nLEDID - 设备ID；
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_Restart":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_Restart(nLEDID);
                break;
            /// <summary>
            /// 3.10	校对时钟
            /// 定义: int LED_AdjustCurrentTime( int nLEDID );
            /// 功能: 对指定的显示屏进行当前时间的校对，当显示屏显示的时间值不准确时，通过调用这个函数可以恢复当前精确时间
            /// 输入： nLEDID - 设备ID；
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_AdjustCurrentTime":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_AdjustCurrentTime(nLEDID);
                break;
            /// <summary>
            /// 3.11	预加载位图资源
            /// 定义: int LED_PreUploadBMP( int nLEDID, const char *szBmpFile );
            /// 功能: 预先将指定的位图数据上传到指定的LED显示屏的存储器
            /// 输入: nLEDID - 设备ID号
            /// szBmpFile – 上传图片的完整路径(该完整路径为上传操作的机器上的路径，驱动应主动从给定路径上将该图片上传到控制器上) 
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_PreUploadBMP":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                szBmpFile = para["PICTURE"].ToString();
                result = wx.LED_PreUploadBMP(nLEDID, szBmpFile);
                break;

            /// <summary>
            /// 3.12	删除预加载的位图
            /// 定义: int LED_RemoveUploadedBmp( int nLEDID, const char *szBmpFile );
            /// 功能: 从控制器上移除指定文件名的图片文件
            /// 输入: nLEDID – 设备ID号
            /// szBmpFile – 要移除的文件名(该文件名是已经上传到控制器里的图片的文件名称，非完整路径,控制器应能识别出该名称，并将它删除)
            /// 返回： 成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_RemoveUploadedBmp":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                szBmpFile = para["PICTURE"].ToString();
                result = wx.LED_RemoveUploadedBmp(nLEDID, szBmpFile);
                break;
            /// <summary>
            /// 3.13	获取剩余缓存空间
            /// 定义: int LED_GetFreeUploadBmpSpace( int nLEDID );
            /// 功能: 获取指定设备上的剩余的上传图片的缓存空间，以字节为单位
            /// 输入： nLEDID - 设备ID
            /// 返回: 以字节为单位的剩余空间
            /// </summary>
            /// <returns></returns>         
            case "LED_GetFreeUploadBmpSpace":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_GetFreeUploadBmpSpace(nLEDID);
                break;
            /// <summary>
            /// 3.14	屏幕初始化
            /// 定义: int LED_LEDScreenInit( int nLEDID, int nScreenType, int nWidth, int nHeight );
            /// 功能: 在每一次准备向指定屏幕下发数据前，对屏幕进行的初始化操作调用
            /// 输入: nLEDID – 设备ID号
            /// nScreenType – 屏幕类型(目前为异步屏协议，该参数可忽略)
            /// nWidth – 屏幕宽度
            /// nHeight – 屏幕高度
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_LEDScreenInit":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                int nScreenType = System.Convert.ToInt32(para["SCREENTYP"].ToString());
                int nWidth = System.Convert.ToInt32(para["WIDTH"].ToString());
                int nHeight = System.Convert.ToInt32(para["HEIGHT"].ToString());
                result = wx.LED_LEDScreenInit(nLEDID, nScreenType, nWidth, nHeight);
                break;
            /// <summary>
            /// 3.15	  添加一个节目(这里节目的意思是一屏的数据)
            /// 定义: int LED_BeginProgram( int nLEDID,  int lDisplayTime,const char *szBackBMP );
            /// 功能: 向指定的显示屏添加一个节目，如果向显示屏添加了多个节目，那么这些节目将自动轮流切换，切换的时间根据lDisplayTime参数的值，如果szBackBMP为NULL，那么不需要去加载背景图片，如果不为NULL，那么需要根据图片名称去加载一张图片.
            /// 输入: nLEDID – 设备ID号
            /// lDisplayTime – 当前节目的显示时间(以毫秒为单位)
            /// szBackBMP – 背景图片名称
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_BeginProgram":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                int lDisplayTime = System.Convert.ToInt32(para["LEAFTIME"].ToString());
                string szBackBMP = para["BACKGROUDBMP"].ToString();
                if (lDisplayTime < 500)
                {
                    lDisplayTime = 1000;
                }
                wx.LED_BeginProgram(nLEDID, lDisplayTime, szBackBMP);
                break;
            /// <summary>
            /// 3.16	添加文字区块
            /// 定义: void LED_AddTextBlock( int nLEDID, const char *szText, LEDBlock rect,LPFontDescriptor lpFont, int nHAlign,int nVAlign,int nEffectType, int nSpeed, int lTrasnparent );
            /// 功能: 向当前节目里添加一个文本区块
            /// 输入: nLEDID – 设备ID号
            /// szText – 该区块上显示的文本内容
            /// rect – 区块的区域范围
            /// lpFont – 区块的字体结构
            /// nHAlign – 文字水平对齐方式
            /// nVAlign – 文字垂直对齐方式
            /// nEffectType – 区块的特效方式
            /// nSpeed – 特效的速度(nEffectType>=2 and nEffectType<=5 表示移动的速度，nEffectType=6表示闪烁的频率)
            /// lTrasnparent – 是否透明显示
            /// </summary>
            /// <returns></returns>
            case "LED_AddTextBlock":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                szText = para["TEXT"].ToString();
                x1 = System.Convert.ToInt32(para["STARTX"].ToString());
                y1 = System.Convert.ToInt32(para["STARTY"].ToString());
                width = System.Convert.ToInt32(para["WIDTH"].ToString());
                height = System.Convert.ToInt32(para["HEIGHT"].ToString());
                destRect.nStartX = x1;
                destRect.nStartY = y1;
                destRect.nWidth = width;
                destRect.nHeight = height;
                lpFont.szFontName = para["FONTNAME"].ToString(); ;
                lpFont.lFontSize = System.Convert.ToInt32(para["FONTSIZE"].ToString());
                lpFont.lFontColor = System.Convert.ToInt32(para["FONTCOLOR"].ToString());
                lpFont.nBold = System.Convert.ToInt32(para["FONTBOLD"].ToString());
                lpFont.nUnderline = System.Convert.ToInt32(para["FONTUNDERLINE"].ToString());
                lpFont.nStrick = System.Convert.ToInt32(para["FONTSTRICK"].ToString());
                nEffectType = System.Convert.ToInt32(para["EFFECTTYPE"].ToString());
                nSpeed = System.Convert.ToInt32(para["SPEED"].ToString());
                lTrasnparent = System.Convert.ToInt32(para["TRANSPARENT"].ToString());
                wx.LED_AddTextBlock(nLEDID, szText, destRect, ref lpFont, nEffectType, nSpeed, lTrasnparent);
                break;
            /// <summary>
            /// 3.17	添加位图区块
            /// 定义: void LED_AddBmpBlock( int nLEDID,const char *szBmpFile, LEDBlock destRect, LEDBlock sourceRect,int nEffectType,int nSpeed, int  lTrasnparent );
            /// 功能: 在当前节目内添加一个位图区块
            /// 输入: nLEDID – 设备ID号
            /// szBmpFile – 显示的存在于控制器上的位图名称
            /// destRect – 目标区域范围,即把位图显示在那个范围内
            /// sourceRect – 要显示的原图的区域范围
            /// nEffectType – 区块特效方式
            /// nSpeed -特效的速度(nEffectType>=2 and nEffectType<=5 表示移动的速度，nEffectType=6表示闪烁的频率)
            /// lTrasnparent – 背景透明
            /// </summary>
            /// <returns></returns>
            case "LED_AddBmpBlock":
                szBmpFile = "D:/SXDFile/"+para["PICTURE"].ToString();
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());              
                destRect.nStartX = System.Convert.ToInt32(para["STARTX"].ToString());
                destRect.nStartY = System.Convert.ToInt32(para["STARTY"].ToString());
                destRect.nWidth = System.Convert.ToInt32(para["WIDTH"].ToString());
                destRect.nHeight = System.Convert.ToInt32(para["HEIGHT"].ToString());
                sourceRect.nHeight = 32;
                sourceRect.nWidth = 128;
                sourceRect.nStartX = 0;
                sourceRect.nStartY = 0;
                nEffectType = System.Convert.ToInt32(para["EFFECTTYPE"].ToString());
                nSpeed = System.Convert.ToInt32(para["SPEED"].ToString());
                lTrasnparent = System.Convert.ToInt32(para["TRANSPARENT"].ToString());
                wx.LED_AddBmpBlock(nLEDID, szBmpFile, destRect, sourceRect, nEffectType, nSpeed, lTrasnparent);
                break;
            /// <summary>
            /// 3.18	添加时钟区块
            /// 定义: void LED_AddClockBlock(int nLEDID, LEDBlock rect, LPClockDescriptor lpClock,LPFontDescriptor lpFont, int nEffectType, int nSpeed, int  lTrasnparent );
            /// 功能: 在当前节目内添加一个时钟区块
            /// 输入: nLEDID – 设备ID号
            /// rect – 时钟的区域范围
            /// lpClock – 时钟描述结构
            /// lpFont – 时钟显示的字体
            /// 以下3个参数忽略
            /// nEffectType –  
            /// nSpeed – 
            /// lTrasnparent – 
            /// </summary>
            /// <returns></returns>
            case "LED_AddClockBlock":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                x1 = System.Convert.ToInt32(para["X1"].ToString());
                y1 = System.Convert.ToInt32(para["Y1"].ToString());
                width = System.Convert.ToInt32(para["WIDTH"].ToString());
                height = System.Convert.ToInt32(para["HEIGHT"].ToString());
                destRect.nStartX = x1;
                destRect.nStartY = y1;
                destRect.nWidth = width;
                destRect.nHeight = height;
                lpFont.szFontName = para["FONTNAME"].ToString(); ;
                lpFont.lFontSize = System.Convert.ToInt32(para["FONTSIZE"].ToString());
                lpFont.lFontColor = System.Convert.ToInt32(para["FONTCOLOR"].ToString());
                lpFont.nBold = System.Convert.ToInt32(para["FONTBOLD"].ToString());
                lpFont.nUnderline = System.Convert.ToInt32(para["FONTUNDERLINE"].ToString());
                lpFont.nStrick = System.Convert.ToInt32(para["FONTSTRICK"].ToString());
                nEffectType = System.Convert.ToInt32(para["EFFECTTYPE"].ToString());
                nSpeed = System.Convert.ToInt32(para["SPEED"].ToString());
                wx.LED_AddClockBlock(nLEDID, destRect, ref lpClock, ref lpFont, nEffectType, nSpeed, lTrasnparent);
                break;
            /// <summary>
            /// 3.19	结束节目编辑
            /// 定义: long LED_EndProgram( int nLEDID );
            /// 功能: 结束对当前(和最近一次LED_BeginProgram调用匹配)节目的编辑，即表示一屏数据组织已经准备完毕.
            /// 输入: nLEDID – 设备ID号
            /// 返回：成功返回0，失败返回-1
            /// </summary>
            /// <returns></returns>
            case "LED_EndProgram":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                wx.LED_EndProgram(nLEDID);
                break;
            case "LED_SendToScreen":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_SendToScreen(nLEDID);
                break;
            case "LED_FastenProgram":
                nLEDID = System.Convert.ToInt32(para["LEDID"].ToString());
                result = wx.LED_FastenProgram(nLEDID);
                break;
        }
        return result;
    }
}

